 package com.jiankangjin.transsystem.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiankangjin.transsystem.common.CommonResponse;
import com.jiankangjin.transsystem.common.Constant;
import com.jiankangjin.transsystem.search.GetOrderResult;
import com.jiankangjin.transsystem.services.SearchOrderServices;
import com.jiankangjin.transsystem.util.StringUtil;

/**  
 * 分页查询 
 */
@Controller
public class GetOrderResultController {
	private static final transient Log logger = LogFactory.getLog(GetOrderResultController.class);

	@Autowired
	private SearchOrderServices searchOrderServices;
	
	@RequestMapping(value="/get_order_result", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getOrderResult(
			@RequestParam(required = false) String token,
			@RequestParam(required = false) String result_id,
			@RequestParam(required = false) String page,
			@RequestParam(required = false) String quantity,
			@RequestParam(required = false) String sign
			){
		StringBuilder sb = new StringBuilder();
		sb.append("token" + token);
		sb.append(" result_id=" + result_id);
		sb.append(" page=" + page);
		sb.append(" quantity=" + quantity);
		sb.append(" sign=" + sign);
		if (logger.isInfoEnabled()) {
			logger.info("搜索订单结果接口:" + sb);
		}
		// 1. 验证内容
		if (StringUtil.isEmpty(token)) {
			String errorMsg = Constant.RESP_STAT_MSG_0001 + ",请求令牌错误";
			return CommonResponse.formatErrorResult(Constant.RESP_STAT_CODE_0001,errorMsg);
		}
		if (StringUtil.isEmpty(result_id)) {
			String errorMsg = Constant.RESP_STAT_MSG_0001 + ",搜索结果编号错误";
			return CommonResponse.formatErrorResult(Constant.RESP_STAT_CODE_0001,errorMsg);
		}
		int _page = 1;
		if (StringUtil.isNotEmpty(page)) {
			if (StringUtil.isNotNum(page)) {
				String errorMsg = Constant.RESP_STAT_MSG_0001 + ",搜索页码错误";
				return CommonResponse.formatErrorResult(Constant.RESP_STAT_CODE_0001,errorMsg);
			} else {
				_page = Integer.parseInt(page);
			}
		}
		int _quantity = 10;
		if (StringUtil.isNotEmpty(quantity)) {
			if (StringUtil.isNotNum(quantity)) {
				String errorMsg = Constant.RESP_STAT_MSG_0001 + ",搜索记录条数错误";
				return CommonResponse.formatErrorResult(Constant.RESP_STAT_CODE_0001,errorMsg);
			} else {
				_quantity = Integer.parseInt(quantity);
			}
		}
		
		// 2. 验证签名
		
		Map<String, Object> map = new HashMap<>();
		List<GetOrderResult> list = searchOrderServices.getOrderResult(result_id, _page, _quantity);
		map.put("resp_status", "0000");
		map.put("trade", list);
		map.put("trade_quantity", list==null ? 0 : list.size());
		map.put("sign", "transSystem_getOrderRefundResult_sign");
		return map;
	}
}
 